/**
 * 
 */
package com.easidea.tim.service.finance;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.finance.CustomerAccountListingDao;
import com.easidea.tim.dao.finance.FinanceItemDao;
import com.easidea.tim.entity.finance.FinanceItem;

/**
 * @author Harrison
 * 
 */
public class FinanceItemService extends
		EntityService<FinanceItem, FinanceItemDao> {

	private FinanceItemDao financeItemDao;
	private CustomerAccountListingDao customerAccountListingDao;

	public boolean saveFi(FinanceItem fi) {
		boolean t = false;
		if (fi != null && StringUtils.isNotBlank(fi.getName())) {
			fi.setId(null);
			fi.setCreateTime(new Date());
			fi.setModifyTime(new Date());
			financeItemDao.save(fi);
			if (fi.getId() != null)
				t = true;
		}
		return t;
	}

	public boolean alterFi(FinanceItem fi) {
		boolean t = false;
		if (fi != null && fi.getId() != null && fi.getId().intValue() != -1
				&& fi.getName() != null && StringUtils.isNotBlank(fi.getName())) {
			FinanceItem oldFi = financeItemDao.get(fi.getId());
			if (oldFi != null) {
				oldFi.setName(fi.getName());
				oldFi.setDirection(fi.getDirection());
				oldFi.setIsCALRel(fi.getIsCALRel());
				oldFi.setModifier(fi.getModifier());
				oldFi.setModifyTime(new Date());
				
				String hqlUpdate = "update com.easidea.tim.entity.finance.CustomerAccountListing set itemName = '"+fi.getName()+"' where item = " + oldFi.getId();
				int size = customerAccountListingDao.getHibernateTemplate().bulkUpdate(hqlUpdate);
				if(size >= 0){
					t = true;
				}
			}
		}
		return t;
	}

	public boolean removeFI(Long id){
		FinanceItem fi = financeItemDao.get(id);
		if(fi != null){
			financeItemDao.removeById(id);
			return true;
		}else
			return false;
	}
	
	public List<FinanceItem> getItemListByCALRel(Boolean isNeed){
		Criteria cte = createUnDeletedCriteria();
		cte.add(Restrictions.eq("isCALRel", isNeed));
		cte.addOrder(Order.desc("createTime"));
		return cte.list();
	}
	
	
	/**
	 * 
	 * @return
	 */
	public List<FinanceItem> getAllItemList() {
		return financeItemDao.getAllValid();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public FinanceItemDao getEntityDao() {
		return financeItemDao;
	}

	/**
	 * @return the financeItemDao
	 */
	public FinanceItemDao getFinanceItemDao() {
		return financeItemDao;
	}

	/**
	 * @param financeItemDao
	 *            the financeItemDao to set
	 */
	public void setFinanceItemDao(FinanceItemDao financeItemDao) {
		this.financeItemDao = financeItemDao;
	}

	/**
	 * @return the customerAccountListingDao
	 */
	public CustomerAccountListingDao getCustomerAccountListingDao() {
		return customerAccountListingDao;
	}

	/**
	 * @param customerAccountListingDao the customerAccountListingDao to set
	 */
	public void setCustomerAccountListingDao(
			CustomerAccountListingDao customerAccountListingDao) {
		this.customerAccountListingDao = customerAccountListingDao;
	}

}
